<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Admin_Eyeoptions extends Admin_Controller {
	public function __construct()
	{
		parent::__construct();
		$this->lang->load('eyeoption');
	}
	public function ajax_eyeoption_get(){
		$eyeoptionEntity =  new CI_Eye_option_entity();
		$eyeoption = $eyeoptionEntity->getEyeOptionByCode($_REQUEST['code']);
		echo json_encode($eyeoption);
	}
	public function ajax_eyeoption_list(){
		$eyeoptionEntity =  new CI_Eye_option_entity();
		$escortSettings = new CI_Escort_setting_entity();
		$defaultLanguageId = $escortSettings->getDefaultLanguageId();
		$eyeoptionList = $eyeoptionEntity->getEyeOptionListByLanguage($defaultLanguageId);
		$data['eyeoptionList'] = $eyeoptionList;
		
		$languageEntity = new CI_Language_entity();
		$languageList = $languageEntity->getLanguageList();
		$data['languageList'] = $languageList;
		
		$this->load->view('admin/eyeoptions/index', $data);
	}
	public function ajax_eyeoption_save(){
		$eyeoptionNameList = $_REQUEST['eyeoption'];
		$eyeoptionNameDefault = '';
		foreach ($eyeoptionNameList as $key => $value) {
			if($value != null && $value != ""){
				$eyeoptionNameDefault = $value;
				break;
			}
		}
		$result = array();
		if($eyeoptionNameDefault == ''){
			$result['result'] = false;
			$result['message'] = lang('escort_eyeoption_require');
		}else{
			$code = 0;
			foreach ($eyeoptionNameList as $key => $value) {
				$eyeoptionEntity =  new CI_Eye_option_entity();
				if($value != null && $value != ''){
					$eyeoptionEntity->setEye($value);
				}else{
					$eyeoptionEntity->setEye($eyeoptionNameDefault);
				}
				$eyeoptionEntity->setLanguageId($key);
				$eyeoptionEntity->setCode($code);
				$id = $eyeoptionEntity->save();
				if($code == 0){
					$code = $id;
					$eyeoptionEntity->setId($id);
					$eyeoptionEntity->setCode($code);
					$eyeoptionEntity->update();
				}
			}
			$result['result'] = true;
			$result['message'] =lang('escort_eyeoption_save_success');
			$result['id'] =$id;
		}
		echo json_encode($result);
	}
	public function ajax_eyeoption_update(){
		$eyeoptionNameList = $_REQUEST['eyeoption'];
		$eyeoptionIdList = $_REQUEST['eyeoption_id'];
		$eyeoptionCode = $_REQUEST['eyeoption_code'];
		$eyeoptionNameDefault = '';
		foreach ($eyeoptionNameList as $key => $value) {
			if($value != null && $value != ""){
				$eyeoptionNameDefault = $value;
				break;
			}
		}
		$result = array();
		if($eyeoptionNameDefault == ''){
			$result['result'] = false;
			$result['message'] = lang('escort_eyeoption_require');
		}else{
			foreach ($eyeoptionNameList as $key => $value) {
				$eyeoptionEntity =  new CI_Eye_option_entity();
				if($value != null && $value != ''){
					$eyeoptionEntity->setEye($value);
				}else{
					$eyeoptionEntity->setEye($eyeoptionNameDefault);
				}
				$eyeoptionEntity->setLanguageId($key);
				$eyeoptionEntity->setCode($eyeoptionCode);
				$eyeoptionEntity->setId($eyeoptionIdList[$key]);
				$eyeoptionEntity->update();
			}
			$result['result'] = true;
			$result['message'] =lang('escort_eyeoption_save_success');
		}
		
		echo json_encode($result);
	}
	public function ajax_eyeoption_delete(){
		$result = array();
		$eyeoptionEntity =  new CI_Eye_option_entity();
		$result['result'] = $eyeoptionEntity->deleteByCode($_REQUEST['code']);
		if($result['result']){
			$result['message'] = lang('escort_eyeoption_delete_success');
		}else{
			$result['message'] = lang('escort_eyeoption_delete_error');
		}
		echo json_encode($result);
	}
}
?>